<?php

namespace App\Console\Commands;

use App\Models\Category;
use Illuminate\Console\Command;
use QL\QueryList;

class CategoryCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'spider:category';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        for ($i = 1; $i <= 14; $i ++) {
            QueryList::get('http://www.boqii.com/pet-all/reptile/?p=' . $i)->find('.hot_pet_cont dt')->map(function ($item) {
                $img = $item->find('a img');
                $src = $img->attr('src');
                $title = $img->attr('alt');
                $name = pathinfo($src, PATHINFO_BASENAME);
                file_put_contents(public_path('category/' . $name),file_get_contents($src));
                Category::create([
                    'parent_id' => 751,
                    'title' => $title,
                    'file_path' => '/category/' . $name
                ]);
            });
        }

    }
}
